package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketCoupon;
import com.cskaoyan.app.bean.po.MarketCouponExample;
import com.cskaoyan.app.bean.po.MarketCouponUser;
import com.cskaoyan.app.bean.po.MarketCouponUserExample;
import com.cskaoyan.app.mapper.MarketCouponMapper;
import com.cskaoyan.app.mapper.MarketCouponUserMapper;
import com.cskaoyan.app.service.AdminCouponService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminCouponServiceImpl implements AdminCouponService {

    @Autowired
    private MarketCouponMapper couponMapper;

    @Autowired
    private MarketCouponUserMapper couponUserMapper;

    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info, String name, Integer type, Integer status) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();

        if (type != null) {
            criteria.andTypeEqualTo(type.shortValue());
        }
        if (status != null) {
            criteria.andStatusEqualTo(status.shortValue());
        }
        if (!StringsUtil.isEmpty(name)) {
            criteria.andNameLike(StringsUtil.getConcat(name));
        }

        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(StringsUtil.getOrderClause(info.getSort(), info.getOrder()));

        List<MarketCoupon> list = couponMapper.selectByExample(example);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        marketCoupon.setAddTime(new Date());
        marketCoupon.setUpdateTime(new Date());
        couponMapper.insertSelective(marketCoupon);
        return couponMapper.selectByPrimaryKey(marketCoupon.getId());
    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(new Date());
        couponMapper.updateByPrimaryKeySelective(marketCoupon);
        return couponMapper.selectByPrimaryKey(marketCoupon.getId());
    }

    @Override
    public MarketCoupon read(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonData<MarketCouponUser> listUser(BasePageInfo info, Integer couponId, Integer status, Integer userId) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();

        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status.shortValue());
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(StringsUtil.getOrderClause(info.getSort(), info.getOrder()));

        List<MarketCouponUser> list = couponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        marketCoupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(marketCoupon);
    }
}
